<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, height=device-height, user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0">
<meta name="format-detection" content="telephone=no">
<title>奇纪客服</title>
<link rel="stylesheet" href="dist/css/layui.mobile.css">
    <script src="./js/jweixin.js"></script>
</head>
<body>
</body>
<script src="/res/pako/pako.js"></script>
<script src="/res/jquery/jquery.js"></script>

<script src="/myres/util.js"></script>
<script src="/res/tio_js/tiows.js"></script>
<script src="/res/tio_js/tiocookie.js"></script>
<script src="/res/tio_js/tiows.js"></script>
<script src="/myres/common.js"></script>

<script src="/im/site/SiteImCommand.js" type="text/javascript" ></script>
<script src="/im/site/SiteImHandler.js" type="text/javascript" ></script>
<script src="/im/site/SiteImBsHandler.js" type="text/javascript" ></script>
<script src="/im/site/siteim.js" type="text/javascript" ></script>
<script src="src/layui.js" type="text/javascript"></script>
<script src="msgHandler.js"></script>
<script src="im.js" type="text/javascript"></script>
<script type="text/javascript">
var tool = [{
                    alias: 'fm-img'
                    ,title: '图片'
                    ,iconUnicode: '&#x1f4f7;'
    }];

 var  moreList1= [];

 // im事件
 function moreList (obj){    //监听moreList
       }



       //history message
function chatlog(data,url){
     if(userId>200000000)
window.location.href = "./historyMessage.html?"+mine.id+"&"+data.id+"&consultant";
     else
         window.location.href = "./historyMessage.html?"+mine.id+"&"+data.id+"&member";
 }



 let im_handler = { moreList,sendMessage,chatlog};







 var chatMan;//聊天界面的个人信息
 var openId=location.search.split("?")[1].split("&")[0];
 var userType=location.search.split("?")[1].split("&")[1];
 var cid=location.search.split("?")[1].split("&")[2]||null;


//登录后的初始状态
  function im_load(){
      layim.chat(chatMan);
  }

      var init_data;
      ajax.get('/qj/kf/init',{
          async:false,
          data:{openId},
          success:function(res){

              console.log(JSON.stringify(res)+"kf  init.......")
              init_data=res.data;
              console.log(res.data);
              ajax.get("/login",{//登录
                  data:{
                      loginname:init_data.mine.id
                  },async:false,
                  success:function(res){
                      console.log(res);
                      setTimeout(function(){
                          siteim.send(SiteImCommand.KF_JOIN_P2P_REQ,{
                              uid:init_data.mine.id
                          })
                      },1000)
                  }
              })
              if(init_data.mine.type=="c"){//顾问
                  console.log("consultant");
                  tool.push({
                      alias:'timeover'
                      ,title:'结束'
                      ,iconUnicode:'&#x1007;'
                  });
                  Object.assign(im_handler,consultant_tool);
                  window.userId=parseInt(init_data.mine.id)+200000000;
              }else {//member
                  console.log("member");
                  window.userId = parseInt(init_data.mine.id) + 100000000;
                  /**
                   * 判断是否在有效时间内，如果在有效时间内，则进入该顾问的聊天页面
                   */
              //     if (flag == "consultant") {
              //     $.ajax({
              //         url: '/qj/kf/redirect',
              //         type: 'get',
              //         async: false,
              //         data: {
              //             userId: parseInt(init_data.mine.id),
              //             consultant_id:cid
              //         },
              //         success: function (mb) {
              //             console.log("mb"+mb);
              //             if (mb == 0) {
              //                 wx.miniProgram.getEnv(function(res){
              //                     wx.miniProgram.switchTab({url:'../consult/consult'})
              //                 })
              //             } else {
              //                 for(ia=0;ia<init_data.friend[0].list.length;ia++){
              //                     let item=init_data.friend[0].list[ia];
              //                     if(item.id==mb) {
              //                         chatMan = {
              //                             name: item.username,
              //                             type: "friend",
              //                             avatar: item.avatar,
              //                             id: mb
              //                         }
              //                         window.document.title="奇纪顾问";
              //                         break;
              //                     }
              //                     if(ia==init_data.friend[0].list.length-1){
              //                         alert("last")
              //                         wx.miniProgram.getEnv(function(res){
              //                             wx.miniProgram.switchTab({url:'../consult/consult'})
              //                         })
              //                     }
              //                 }
              //             }
              //         }, error: function (msg) {
              //             console.log(msg);
              //         }
              //     })
              // }else{//否则进入客服页面
              //         chatMan={
              //             name:init_data.friend[1].list[0].username,
              //             avatar:init_data.friend[1].list[0].avatar,
              //             type:'friend',
              //             id:init_data.friend[1].list[0].id
              //         }
              //     }
              }
              fm.im.init(init_data,tool,moreList1,im_handler,im_load);
          }
      })


  var   receiveMsg = function(item){
          var model={
          username: item.fromUser.username
          ,avatar: item.fromUser.avatar
          ,id: item.fromId
          ,type: "friend" //聊天窗口来源类型，从发送消息传递的to里面获取
          ,cid:item.mid
          ,content: item.content //消息内容
          ,mine: false //是否我发送的消息，如果为true，则会显示在右方
          ,timestamp:item.time //服务端动态时间戳
            }
          layim.getMessage(model);
          var req={
              cid:item.mid,
              toId:item.fromId
          }
          siteim.send(SiteImCommand.KF_P2P_READ_REQ,req)
  }

</script>
<script src="wx.js"></script>
<script src="msgHandler.js"></script>
</html>
